<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Test2::API::Context - Object to represent a testing context. - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/Test2::API::Context">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./Test2::API::Context.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/Test2::API::Context">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/Test2::API::Context">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/Test2::API::Context">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/Test2::API::Context">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/Test2::API::Context">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/Test2::API::Context">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/Test2::API::Context">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/Test2::API::Context">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/Test2::API::Context">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/Test2::API::Context">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/Test2::API::Context">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/Test2::API::Context">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/Test2::API::Context">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/Test2::API::Context">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/Test2::API::Context">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/Test2::API::Context">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/Test2::API::Context">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/Test2::API::Context">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/Test2::API::Context">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/Test2::API::Context">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/Test2::API::Context">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/Test2::API::Context">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/Test2::API::Context">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/Test2::API::Context">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/Test2::API::Context">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/Test2::API::Context">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/Test2::API::Context">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/Test2::API::Context">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/Test2::API::Context">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/Test2::API::Context">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/Test2::API::Context">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/Test2::API::Context">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/Test2::API::Context">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/Test2::API::Context">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/Test2::API::Context">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/Test2::API::Context">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/Test2::API::Context">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/Test2::API::Context">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/Test2::API::Context">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/Test2::API::Context">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/Test2::API::Context">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/Test2::API::Context">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/Test2::API::Context">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/Test2::API::Context">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/Test2::API::Context">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/Test2::API::Context">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/Test2::API::Context">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/Test2::API::Context">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/Test2::API::Context">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/Test2::API::Context">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/Test2::API::Context">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/Test2::API::Context">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/Test2::API::Context">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/Test2::API::Context">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/Test2::API::Context">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/Test2::API::Context">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/Test2::API::Context">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/Test2::API::Context">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/Test2::API::Context">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/Test2::API::Context">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/Test2::API::Context">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/Test2::API::Context">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/Test2::API::Context">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/Test2::API::Context">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/Test2::API::Context">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/Test2::API::Context">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/Test2::API::Context">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/Test2::API::Context">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/Test2::API::Context">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/Test2::API::Context">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/Test2::API::Context">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/Test2::API::Context">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/Test2::API::Context">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/Test2::API::Context">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/Test2::API::Context">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/Test2::API::Context">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/Test2::API::Context">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/Test2::API::Context">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/Test2::API::Context">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/Test2::API::Context">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./Test2::API::Context.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/Test2::API::Context">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/Test2::API::Context">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/Test2::API::Context">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/Test2::API::Context">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/Test2::API::Context">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/Test2::API::Context">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/Test2::API::Context">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/Test2::API::Context">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/Test2::API::Context">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/Test2::API::Context">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/Test2::API::Context">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/Test2::API::Context">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/Test2::API::Context">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/Test2::API::Context">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/Test2::API::Context">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/Test2::API::Context">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/Test2::API::Context">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/Test2::API::Context">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/Test2::API::Context">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/Test2::API::Context">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/Test2::API::Context">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/Test2::API::Context">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/Test2::API::Context">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/Test2::API::Context">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/Test2::API::Context">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/Test2::API::Context">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/Test2::API::Context">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/Test2::API::Context">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/Test2::API::Context">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/Test2::API::Context">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/Test2::API::Context">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/Test2::API::Context">5.35.1</a>
          <a class="dropdown-item active" href="./Test2::API::Context.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./Test2::API::Context.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="./Test2::API::Context.html">Test2::API::Context</a>
          <div id="more">
            (<a href="./Test2::API::Context.txt">source</a>,
            <a href="https://metacpan.org/pod/Test2::API::Context">CPAN</a>)
          </div>
            <div id="moduleversion">version 1.302183</div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#DESCRIPTION">DESCRIPTION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#SYNOPSIS">SYNOPSIS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#CRITICAL-DETAILS">CRITICAL DETAILS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#METHODS">METHODS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#EVENT-PRODUCTION-METHODS">EVENT PRODUCTION METHODS</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#HOOKS">HOOKS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#INIT-HOOKS">INIT HOOKS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#GLOBAL">GLOBAL</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#PER-HUB">PER HUB</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#PER-CONTEXT">PER CONTEXT</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#RELEASE-HOOKS">RELEASE HOOKS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#GLOBAL1">GLOBAL</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#PER-HUB1">PER HUB</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#PER-CONTEXT1">PER CONTEXT</a>
              </li>
          </ul>

              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#THIRD-PARTY-META-DATA">THIRD PARTY META-DATA</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#SOURCE">SOURCE</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#MAINTAINERS">MAINTAINERS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#AUTHORS">AUTHORS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Test2::API::Context.html#COPYRIGHT">COPYRIGHT</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="./Test2::API::Context.html#NAME">#</a>NAME</h1>

<p>Test2::API::Context - Object to represent a testing context.</p>

<h1 id="DESCRIPTION"><a class="permalink" href="./Test2::API::Context.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>The context object is the primary interface for authors of testing tools written with <a href="Test2.html">Test2</a>. The context object represents the context in which a test takes place (File and Line Number), and provides a quick way to generate events from that context. The context object also takes care of sending events to the correct <a href="./Test2::Hub.html">Test2::Hub</a> instance.</p>

<h1 id="SYNOPSIS"><a class="permalink" href="./Test2::API::Context.html#SYNOPSIS">#</a>SYNOPSIS</h1>

<p>In general you will not be creating contexts directly. To obtain a context you should always use <code>context()</code> which is exported by the <a href="./Test2::API.html">Test2::API</a> module.</p>

<pre><code>use Test2::API qw/context/;

sub my_ok {
    my ($bool, $name) = @_;
    my $ctx = context();

    if ($bool) {
        $ctx-&gt;pass($name);
    }
    else {
        $ctx-&gt;fail($name);
    }

    $ctx-&gt;release; # You MUST do this!
    return $bool;
}</code></pre>

<p>Context objects make it easy to wrap other tools that also use context. Once you grab a context, any tool you call before releasing your context will inherit it:</p>

<pre><code>sub wrapper {
    my ($bool, $name) = @_;
    my $ctx = context();
    $ctx-&gt;diag(&quot;wrapping my_ok&quot;);

    my $out = my_ok($bool, $name);
    $ctx-&gt;release; # You MUST do this!
    return $out;
}</code></pre>

<h1 id="CRITICAL-DETAILS"><a class="permalink" href="./Test2::API::Context.html#CRITICAL-DETAILS">#</a><a id="CRITICAL"></a>CRITICAL DETAILS</h1>

<dl>

<dt id="you-MUST-always-use-the-context()-sub-from-Test2::API"><a class="permalink" href="./Test2::API::Context.html#you-MUST-always-use-the-context()-sub-from-Test2::API">#</a><a id="you"></a><a id="you-MUST-always-use-the-context-sub-from-Test2::API"></a>you MUST always use the context() sub from Test2::API</dt>
<dd>

<p>Creating your own context via <code>Test2::API::Context-&gt;new()</code> will almost never produce a desirable result. Use <code>context()</code> which is exported by <a href="./Test2::API.html">Test2::API</a>.</p>

<p>There are a handful of cases where a tool author may want to create a new context by hand, which is why the <code>new</code> method exists. Unless you really know what you are doing you should avoid this.</p>

</dd>
<dt id="You-MUST-always-release-the-context-when-done-with-it"><a class="permalink" href="./Test2::API::Context.html#You-MUST-always-release-the-context-when-done-with-it">#</a><a id="You"></a>You MUST always release the context when done with it</dt>
<dd>

<p>Releasing the context tells the system you are done with it. This gives it a chance to run any necessary callbacks or cleanup tasks. If you forget to release the context it will try to detect the problem and warn you about it.</p>

</dd>
<dt id="You-MUST-NOT-pass-context-objects-around"><a class="permalink" href="./Test2::API::Context.html#You-MUST-NOT-pass-context-objects-around">#</a><a id="You1"></a>You MUST NOT pass context objects around</dt>
<dd>

<p>When you obtain a context object it is made specifically for your tool and any tools nested within. If you pass a context around you run the risk of polluting other tools with incorrect context information.</p>

<p>If you are certain that you want a different tool to use the same context you may pass it a snapshot. <code>$ctx-&gt;snapshot</code> will give you a shallow clone of the context that is safe to pass around or store.</p>

</dd>
<dt id="You-MUST-NOT-store-or-cache-a-context-for-later"><a class="permalink" href="./Test2::API::Context.html#You-MUST-NOT-store-or-cache-a-context-for-later">#</a><a id="You2"></a>You MUST NOT store or cache a context for later</dt>
<dd>

<p>As long as a context exists for a given hub, all tools that try to get a context will get the existing instance. If you try to store the context you will pollute other tools with incorrect context information.</p>

<p>If you are certain that you want to save the context for later, you can use a snapshot. <code>$ctx-&gt;snapshot</code> will give you a shallow clone of the context that is safe to pass around or store.</p>

<p><code>context()</code> has some mechanisms to protect you if you do cause a context to persist beyond the scope in which it was obtained. In practice you should not rely on these protections, and they are fairly noisy with warnings.</p>

</dd>
<dt id="You-SHOULD-obtain-your-context-as-soon-as-possible-in-a-given-tool"><a class="permalink" href="./Test2::API::Context.html#You-SHOULD-obtain-your-context-as-soon-as-possible-in-a-given-tool">#</a><a id="You3"></a>You SHOULD obtain your context as soon as possible in a given tool</dt>
<dd>

<p>You never know what tools you call from within your own tool will need a context. Obtaining the context early ensures that nested tools can find the context you want them to find.</p>

</dd>
</dl>

<h1 id="METHODS"><a class="permalink" href="./Test2::API::Context.html#METHODS">#</a>METHODS</h1>

<dl>

<dt id="$ctx-&gt;done_testing;"><a class="permalink" href="./Test2::API::Context.html#$ctx-%3Edone_testing;">#</a><a id="done_testing"></a><a id="ctx-done_testing"></a>$ctx-&gt;done_testing;</dt>
<dd>

<p>Note that testing is finished. If no plan has been set this will generate a Plan event.</p>

</dd>
<dt id="$clone-=-$ctx-&gt;snapshot()"><a class="permalink" href="./Test2::API::Context.html#$clone-=-$ctx-%3Esnapshot()">#</a><a id="clone-ctx-snapshot"></a>$clone = $ctx-&gt;snapshot()</dt>
<dd>

<p>This will return a shallow clone of the context. The shallow clone is safe to store for later.</p>

</dd>
<dt id="$ctx-&gt;release()"><a class="permalink" href="./Test2::API::Context.html#$ctx-%3Erelease()">#</a><a id="release"></a><a id="ctx-release"></a>$ctx-&gt;release()</dt>
<dd>

<p>This will release the context. This runs cleanup tasks, and several important hooks. It will also restore <code>$!</code>, <code>$?</code>, and <code>$@</code> to what they were when the context was created.</p>

<p><b>Note:</b> If a context is acquired more than once an internal refcount is kept. <code>release()</code> decrements the ref count, none of the other actions of <code>release()</code> will occur unless the refcount hits 0. This means only the last call to <code>release()</code> will reset <code>$?</code>, <code>$!</code>, <code>$@</code>,and run the cleanup tasks.</p>

</dd>
<dt id="$ctx-&gt;throw($message)"><a class="permalink" href="./Test2::API::Context.html#$ctx-%3Ethrow($message)">#</a><a id="throw"></a><a id="ctx-throw-message"></a>$ctx-&gt;throw($message)</dt>
<dd>

<p>This will throw an exception reporting to the file and line number of the context. This will also release the context for you.</p>

</dd>
<dt id="$ctx-&gt;alert($message)"><a class="permalink" href="./Test2::API::Context.html#$ctx-%3Ealert($message)">#</a><a id="alert"></a><a id="ctx-alert-message"></a>$ctx-&gt;alert($message)</dt>
<dd>

<p>This will issue a warning from the file and line number of the context.</p>

</dd>
<dt id="$stack-=-$ctx-&gt;stack()"><a class="permalink" href="./Test2::API::Context.html#$stack-=-$ctx-%3Estack()">#</a><a id="stack-ctx-stack"></a>$stack = $ctx-&gt;stack()</dt>
<dd>

<p>This will return the <a href="./Test2::API::Stack.html">Test2::API::Stack</a> instance the context used to find the current hub.</p>

</dd>
<dt id="$hub-=-$ctx-&gt;hub()"><a class="permalink" href="./Test2::API::Context.html#$hub-=-$ctx-%3Ehub()">#</a><a id="hub-ctx-hub"></a>$hub = $ctx-&gt;hub()</dt>
<dd>

<p>This will return the <a href="./Test2::Hub.html">Test2::Hub</a> instance the context recognizes as the current one to which all events should be sent.</p>

</dd>
<dt id="$dbg-=-$ctx-&gt;trace()"><a class="permalink" href="./Test2::API::Context.html#$dbg-=-$ctx-%3Etrace()">#</a><a id="dbg-ctx-trace"></a>$dbg = $ctx-&gt;trace()</dt>
<dd>

<p>This will return the <a href="./Test2::EventFacet::Trace.html">Test2::EventFacet::Trace</a> instance used by the context.</p>

</dd>
<dt id="$ctx-&gt;do_in_context(\&amp;code,-@args);"><a class="permalink" href="./Test2::API::Context.html#$ctx-%3Edo_in_context(%5C&amp;code,-@args);">#</a><a id="do_in_context"></a><a id="ctx-do_in_context-code-args"></a>$ctx-&gt;do_in_context(\&amp;code, @args);</dt>
<dd>

<p>Sometimes you have a context that is not current, and you want things to use it as the current one. In these cases you can call <code>$ctx-&gt;do_in_context(sub { ... })</code>. The codeblock will be run, and anything inside of it that looks for a context will find the one on which the method was called.</p>

<p>This <b>DOES NOT</b> affect context on other hubs, only the hub used by the context will be affected.</p>

<pre><code>my $ctx = ...;
$ctx-&gt;do_in_context(sub {
    my $ctx = context(); # returns the $ctx the sub is called on
});</code></pre>

<p><b>Note:</b> The context will actually be cloned, the clone will be used instead of the original. This allows the thread id, process id, and error variables to be correct without modifying the original context.</p>

</dd>
<dt id="$ctx-&gt;restore_error_vars()"><a class="permalink" href="./Test2::API::Context.html#$ctx-%3Erestore_error_vars()">#</a><a id="restore_error_vars"></a><a id="ctx-restore_error_vars"></a>$ctx-&gt;restore_error_vars()</dt>
<dd>

<p>This will set <code>$!</code>, <code>$?</code>, and <code>$@</code> to what they were when the context was created. There is no localization or anything done here, calling this method will actually set these vars.</p>

</dd>
<dt id="$!-=-$ctx-&gt;errno()"><a class="permalink" href="./Test2::API::Context.html#$!-=-$ctx-%3Eerrno()">#</a><a id="ctx-errno"></a>$! = $ctx-&gt;errno()</dt>
<dd>

<p>The (numeric) value of <code>$!</code> when the context was created.</p>

</dd>
<dt id="$?-=-$ctx-&gt;child_error()"><a class="permalink" href="./Test2::API::Context.html#$?-=-$ctx-%3Echild_error()">#</a><a id="ctx-child_error"></a>$? = $ctx-&gt;child_error()</dt>
<dd>

<p>The value of <code>$?</code> when the context was created.</p>

</dd>
<dt id="$@-=-$ctx-&gt;eval_error()"><a class="permalink" href="./Test2::API::Context.html#$@-=-$ctx-%3Eeval_error()">#</a><a id="ctx-eval_error"></a>$@ = $ctx-&gt;eval_error()</dt>
<dd>

<p>The value of <code>$@</code> when the context was created.</p>

</dd>
</dl>

<h2 id="EVENT-PRODUCTION-METHODS"><a class="permalink" href="./Test2::API::Context.html#EVENT-PRODUCTION-METHODS">#</a><a id="EVENT"></a>EVENT PRODUCTION METHODS</h2>

<p><b>Which one do I use?</b></p>

<p>The <code>pass*</code> and <code>fail*</code> are optimal if they meet your situation, using one of them will always be the most optimal. That said they are optimal by eliminating many features.</p>

<p>Method such as <code>ok</code>, and <code>note</code> are shortcuts for generating common 1-task events based on the old API, however they are forward compatible, and easy to use. If these meet your needs then go ahead and use them, but please check back often for alternatives that may be added.</p>

<p>If you want to generate new style events, events that do many things at once, then you want the <code>*ev2*</code> methods. These let you directly specify which facets you wish to use.</p>

<dl>

<dt id="$event-=-$ctx-&gt;pass()"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Epass()">#</a><a id="event-ctx-pass"></a>$event = $ctx-&gt;pass()</dt>
<dd>

</dd>
<dt id="$event-=-$ctx-&gt;pass($name)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Epass($name)">#</a><a id="event-ctx-pass-name"></a>$event = $ctx-&gt;pass($name)</dt>
<dd>

<p>This will send and return an <a href="./Test2::Event::Pass.html">Test2::Event::Pass</a> event. You may optionally provide a <code>$name</code> for the assertion.</p>

<p>The <a href="./Test2::Event::Pass.html">Test2::Event::Pass</a> is a specially crafted and optimized event, using this will help the performance of passing tests.</p>

</dd>
<dt id="$true-=-$ctx-&gt;pass_and_release()"><a class="permalink" href="./Test2::API::Context.html#$true-=-$ctx-%3Epass_and_release()">#</a><a id="true-ctx-pass_and_release"></a>$true = $ctx-&gt;pass_and_release()</dt>
<dd>

</dd>
<dt id="$true-=-$ctx-&gt;pass_and_release($name)"><a class="permalink" href="./Test2::API::Context.html#$true-=-$ctx-%3Epass_and_release($name)">#</a><a id="true-ctx-pass_and_release-name"></a>$true = $ctx-&gt;pass_and_release($name)</dt>
<dd>

<p>This is a combination of <code>pass()</code> and <code>release()</code>. You can use this if you do not plan to do anything with the context after sending the event. This helps write more clear and compact code.</p>

<pre><code>sub shorthand {
    my ($bool, $name) = @_;
    my $ctx = context();
    return $ctx-&gt;pass_and_release($name) if $bool;

    ... Handle a failure ...
}

sub longform {
    my ($bool, $name) = @_;
    my $ctx = context();

    if ($bool) {
        $ctx-&gt;pass($name);
        $ctx-&gt;release;
        return 1;
    }

    ... Handle a failure ...
}</code></pre>

</dd>
<dt id="my-$event-=-$ctx-&gt;fail()"><a class="permalink" href="./Test2::API::Context.html#my-$event-=-$ctx-%3Efail()">#</a><a id="my"></a><a id="my-event-ctx-fail"></a>my $event = $ctx-&gt;fail()</dt>
<dd>

</dd>
<dt id="my-$event-=-$ctx-&gt;fail($name)"><a class="permalink" href="./Test2::API::Context.html#my-$event-=-$ctx-%3Efail($name)">#</a><a id="my1"></a><a id="my-event-ctx-fail-name"></a>my $event = $ctx-&gt;fail($name)</dt>
<dd>

</dd>
<dt id="my-$event-=-$ctx-&gt;fail($name,-@diagnostics)"><a class="permalink" href="./Test2::API::Context.html#my-$event-=-$ctx-%3Efail($name,-@diagnostics)">#</a><a id="my2"></a><a id="my-event-ctx-fail-name-diagnostics"></a>my $event = $ctx-&gt;fail($name, @diagnostics)</dt>
<dd>

<p>This lets you send an <a href="./Test2::Event::Fail.html">Test2::Event::Fail</a> event. You may optionally provide a <code>$name</code> and <code>@diagnostics</code> messages.</p>

<p>Diagnostics messages can be simple strings, data structures, or instances of <a href="./Test2::EventFacet::Info::Table.html">Test2::EventFacet::Info::Table</a> (which are converted inline into the <a href="./Test2::EventFacet::Info.html">Test2::EventFacet::Info</a> structure).</p>

</dd>
<dt id="my-$false-=-$ctx-&gt;fail_and_release()"><a class="permalink" href="./Test2::API::Context.html#my-$false-=-$ctx-%3Efail_and_release()">#</a><a id="my3"></a><a id="my-false-ctx-fail_and_release"></a>my $false = $ctx-&gt;fail_and_release()</dt>
<dd>

</dd>
<dt id="my-$false-=-$ctx-&gt;fail_and_release($name)"><a class="permalink" href="./Test2::API::Context.html#my-$false-=-$ctx-%3Efail_and_release($name)">#</a><a id="my4"></a><a id="my-false-ctx-fail_and_release-name"></a>my $false = $ctx-&gt;fail_and_release($name)</dt>
<dd>

</dd>
<dt id="my-$false-=-$ctx-&gt;fail_and_release($name,-@diagnostics)"><a class="permalink" href="./Test2::API::Context.html#my-$false-=-$ctx-%3Efail_and_release($name,-@diagnostics)">#</a><a id="my5"></a><a id="my-false-ctx-fail_and_release-name-diagnostics"></a>my $false = $ctx-&gt;fail_and_release($name, @diagnostics)</dt>
<dd>

<p>This is a combination of <code>fail()</code> and <code>release()</code>. This can be used to write clearer and shorter code.</p>

<pre><code>sub shorthand {
    my ($bool, $name) = @_;
    my $ctx = context();
    return $ctx-&gt;fail_and_release($name) unless $bool;

    ... Handle a success ...
}

sub longform {
    my ($bool, $name) = @_;
    my $ctx = context();

    unless ($bool) {
        $ctx-&gt;pass($name);
        $ctx-&gt;release;
        return 1;
    }

    ... Handle a success ...
}</code></pre>

</dd>
<dt id="$event-=-$ctx-&gt;ok($bool,-$name)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Eok($bool,-$name)">#</a><a id="event-ctx-ok-bool-name"></a>$event = $ctx-&gt;ok($bool, $name)</dt>
<dd>

</dd>
<dt id="$event-=-$ctx-&gt;ok($bool,-$name,-\@on_fail)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Eok($bool,-$name,-%5C@on_fail)">#</a><a id="event-ctx-ok-bool-name-on_fail"></a>$event = $ctx-&gt;ok($bool, $name, \@on_fail)</dt>
<dd>

<p><b>NOTE:</b> Use of this method is discouraged in favor of <code>pass()</code> and <code>fail()</code> which produce <a href="./Test2::Event::Pass.html">Test2::Event::Pass</a> and <a href="./Test2::Event::Fail.html">Test2::Event::Fail</a> events. These newer event types are faster and less crufty.</p>

<p>This will create an <a href="./Test2::Event::Ok.html">Test2::Event::Ok</a> object for you. If <code>$bool</code> is false then an <a href="./Test2::Event::Diag.html">Test2::Event::Diag</a> event will be sent as well with details about the failure. If you do not want automatic diagnostics you should use the <code>send_event()</code> method directly.</p>

<p>The third argument <code>\@on_fail</code>) is an optional set of diagnostics to be sent in the event of a test failure. Unlike with <code>fail()</code> these diagnostics must be plain strings, data structures are not supported.</p>

</dd>
<dt id="$event-=-$ctx-&gt;note($message)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Enote($message)">#</a><a id="event-ctx-note-message"></a>$event = $ctx-&gt;note($message)</dt>
<dd>

<p>Send an <a href="./Test2::Event::Note.html">Test2::Event::Note</a>. This event prints a message to STDOUT.</p>

</dd>
<dt id="$event-=-$ctx-&gt;diag($message)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Ediag($message)">#</a><a id="event-ctx-diag-message"></a>$event = $ctx-&gt;diag($message)</dt>
<dd>

<p>Send an <a href="./Test2::Event::Diag.html">Test2::Event::Diag</a>. This event prints a message to STDERR.</p>

</dd>
<dt id="$event-=-$ctx-&gt;plan($max)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Eplan($max)">#</a><a id="event-ctx-plan-max"></a>$event = $ctx-&gt;plan($max)</dt>
<dd>

</dd>
<dt id="$event-=-$ctx-&gt;plan(0,-&#39;SKIP&#39;,-$reason)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Eplan(0,-&#39;SKIP&#39;,-$reason)">#</a><a id="event-ctx-plan-0-SKIP-reason"></a>$event = $ctx-&gt;plan(0, &#39;SKIP&#39;, $reason)</dt>
<dd>

<p>This can be used to send an <a href="./Test2::Event::Plan.html">Test2::Event::Plan</a> event. This event usually takes either a number of tests you expect to run. Optionally you can set the expected count to 0 and give the &#39;SKIP&#39; directive with a reason to cause all tests to be skipped.</p>

</dd>
<dt id="$event-=-$ctx-&gt;skip($name,-$reason);"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Eskip($name,-$reason);">#</a><a id="event-ctx-skip-name-reason"></a>$event = $ctx-&gt;skip($name, $reason);</dt>
<dd>

<p>Send an <a href="./Test2::Event::Skip.html">Test2::Event::Skip</a> event.</p>

</dd>
<dt id="$event-=-$ctx-&gt;bail($reason)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Ebail($reason)">#</a><a id="event-ctx-bail-reason"></a>$event = $ctx-&gt;bail($reason)</dt>
<dd>

<p>This sends an <a href="./Test2::Event::Bail.html">Test2::Event::Bail</a> event. This event will completely terminate all testing.</p>

</dd>
<dt id="$event-=-$ctx-&gt;send_ev2(%facets)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Esend_ev2(%25facets)">#</a><a id="event-ctx-send_ev2-facets"></a>$event = $ctx-&gt;send_ev2(%facets)</dt>
<dd>

<p>This lets you build and send a V2 event directly from facets. The event is returned after it is sent.</p>

<p>This example sends a single assertion, a note (comment for stdout in Test::Builder talk) and sets the plan to 1.</p>

<pre><code>my $event = $ctx-&gt;send_event(
    plan   =&gt; {count =&gt; 1},
    assert =&gt; {pass  =&gt; 1, details =&gt; &quot;A passing assert&quot;},
    info =&gt; [{tag =&gt; &#39;NOTE&#39;, details =&gt; &quot;This is a note&quot;}],
);</code></pre>

</dd>
<dt id="$event-=-$ctx-&gt;build_e2(%facets)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Ebuild_e2(%25facets)">#</a><a id="event-ctx-build_e2-facets"></a>$event = $ctx-&gt;build_e2(%facets)</dt>
<dd>

<p>This is the same as <code>send_ev2()</code>, except it builds and returns the event without sending it.</p>

</dd>
<dt id="$event-=-$ctx-&gt;send_ev2_and_release($Type,-%parameters)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Esend_ev2_and_release($Type,-%25parameters)">#</a><a id="event-ctx-send_ev2_and_release-Type-parameters"></a>$event = $ctx-&gt;send_ev2_and_release($Type, %parameters)</dt>
<dd>

<p>This is a combination of <code>send_ev2()</code> and <code>release()</code>.</p>

<pre><code>sub shorthand {
    my $ctx = context();
    return $ctx-&gt;send_ev2_and_release(assert =&gt; {pass =&gt; 1, details =&gt; &#39;foo&#39;});
}

sub longform {
    my $ctx = context();
    my $event = $ctx-&gt;send_ev2(assert =&gt; {pass =&gt; 1, details =&gt; &#39;foo&#39;});
    $ctx-&gt;release;
    return $event;
}</code></pre>

</dd>
<dt id="$event-=-$ctx-&gt;send_event($Type,-%parameters)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Esend_event($Type,-%25parameters)">#</a><a id="event-ctx-send_event-Type-parameters"></a>$event = $ctx-&gt;send_event($Type, %parameters)</dt>
<dd>

<p><b>It is better to use send_ev2() in new code.</b></p>

<p>This lets you build and send an event of any type. The <code>$Type</code> argument should be the event package name with <code>Test2::Event::</code> left off, or a fully qualified package name prefixed with a &#39;+&#39;. The event is returned after it is sent.</p>

<pre><code>my $event = $ctx-&gt;send_event(&#39;Ok&#39;, ...);</code></pre>

<p>or</p>

<pre><code>my $event = $ctx-&gt;send_event(&#39;+Test2::Event::Ok&#39;, ...);</code></pre>

</dd>
<dt id="$event-=-$ctx-&gt;build_event($Type,-%parameters)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Ebuild_event($Type,-%25parameters)">#</a><a id="event-ctx-build_event-Type-parameters"></a>$event = $ctx-&gt;build_event($Type, %parameters)</dt>
<dd>

<p><b>It is better to use build_ev2() in new code.</b></p>

<p>This is the same as <code>send_event()</code>, except it builds and returns the event without sending it.</p>

</dd>
<dt id="$event-=-$ctx-&gt;send_event_and_release($Type,-%parameters)"><a class="permalink" href="./Test2::API::Context.html#$event-=-$ctx-%3Esend_event_and_release($Type,-%25parameters)">#</a><a id="event-ctx-send_event_and_release-Type-parameters"></a>$event = $ctx-&gt;send_event_and_release($Type, %parameters)</dt>
<dd>

<p><b>It is better to use send_ev2_and_release() in new code.</b></p>

<p>This is a combination of <code>send_event()</code> and <code>release()</code>.</p>

<pre><code>sub shorthand {
    my $ctx = context();
    return $ctx-&gt;send_event_and_release(Pass =&gt; { name =&gt; &#39;foo&#39; });
}

sub longform {
    my $ctx = context();
    my $event = $ctx-&gt;send_event(Pass =&gt; { name =&gt; &#39;foo&#39; });
    $ctx-&gt;release;
    return $event;
}</code></pre>

</dd>
</dl>

<h1 id="HOOKS"><a class="permalink" href="./Test2::API::Context.html#HOOKS">#</a>HOOKS</h1>

<p>There are 2 types of hooks, init hooks, and release hooks. As the names suggest, these hooks are triggered when contexts are created or released.</p>

<h2 id="INIT-HOOKS"><a class="permalink" href="./Test2::API::Context.html#INIT-HOOKS">#</a><a id="INIT"></a>INIT HOOKS</h2>

<p>These are called whenever a context is initialized. That means when a new instance is created. These hooks are <b>NOT</b> called every time something requests a context, just when a new one is created.</p>

<h3 id="GLOBAL"><a class="permalink" href="./Test2::API::Context.html#GLOBAL">#</a>GLOBAL</h3>

<p>This is how you add a global init callback. Global callbacks happen for every context for any hub or stack.</p>

<pre><code>Test2::API::test2_add_callback_context_init(sub {
    my $ctx = shift;
    ...
});</code></pre>

<h3 id="PER-HUB"><a class="permalink" href="./Test2::API::Context.html#PER-HUB">#</a><a id="PER"></a>PER HUB</h3>

<p>This is how you add an init callback for all contexts created for a given hub. These callbacks will not run for other hubs.</p>

<pre><code>$hub-&gt;add_context_init(sub {
    my $ctx = shift;
    ...
});</code></pre>

<h3 id="PER-CONTEXT"><a class="permalink" href="./Test2::API::Context.html#PER-CONTEXT">#</a><a id="PER1"></a>PER CONTEXT</h3>

<p>This is how you specify an init hook that will only run if your call to <code>context()</code> generates a new context. The callback will be ignored if <code>context()</code> is returning an existing context.</p>

<pre><code>my $ctx = context(on_init =&gt; sub {
    my $ctx = shift;
    ...
});</code></pre>

<h2 id="RELEASE-HOOKS"><a class="permalink" href="./Test2::API::Context.html#RELEASE-HOOKS">#</a><a id="RELEASE"></a>RELEASE HOOKS</h2>

<p>These are called whenever a context is released. That means when the last reference to the instance is about to be destroyed. These hooks are <b>NOT</b> called every time <code>$ctx-&gt;release</code> is called.</p>

<h3 id="GLOBAL1"><a class="permalink" href="./Test2::API::Context.html#GLOBAL1">#</a>GLOBAL</h3>

<p>This is how you add a global release callback. Global callbacks happen for every context for any hub or stack.</p>

<pre><code>Test2::API::test2_add_callback_context_release(sub {
    my $ctx = shift;
    ...
});</code></pre>

<h3 id="PER-HUB1"><a class="permalink" href="./Test2::API::Context.html#PER-HUB1">#</a><a id="PER2"></a>PER HUB</h3>

<p>This is how you add a release callback for all contexts created for a given hub. These callbacks will not run for other hubs.</p>

<pre><code>$hub-&gt;add_context_release(sub {
    my $ctx = shift;
    ...
});</code></pre>

<h3 id="PER-CONTEXT1"><a class="permalink" href="./Test2::API::Context.html#PER-CONTEXT1">#</a><a id="PER3"></a>PER CONTEXT</h3>

<p>This is how you add release callbacks directly to a context. The callback will <b>ALWAYS</b> be added to the context that gets returned, it does not matter if a new one is generated, or if an existing one is returned.</p>

<pre><code>my $ctx = context(on_release =&gt; sub {
    my $ctx = shift;
    ...
});</code></pre>

<h1 id="THIRD-PARTY-META-DATA"><a class="permalink" href="./Test2::API::Context.html#THIRD-PARTY-META-DATA">#</a><a id="THIRD"></a>THIRD PARTY META-DATA</h1>

<p>This object consumes <a href="./Test2::Util::ExternalMeta.html">Test2::Util::ExternalMeta</a> which provides a consistent way for you to attach meta-data to instances of this class. This is useful for tools, plugins, and other extensions.</p>

<h1 id="SOURCE"><a class="permalink" href="./Test2::API::Context.html#SOURCE">#</a>SOURCE</h1>

<p>The source code repository for Test2 can be found at <i>http://github.com/Test-More/test-more/</i>.</p>

<h1 id="MAINTAINERS"><a class="permalink" href="./Test2::API::Context.html#MAINTAINERS">#</a>MAINTAINERS</h1>

<dl>

<dt id="Chad-Granum"><a class="permalink" href="./Test2::API::Context.html#Chad-Granum">#</a><a id="Chad-Granum-&lt;exodist@cpan.org&gt;"></a><a id="Chad"></a><a id="Chad-Granum-exodist-cpan.org"></a>Chad Granum &lt;exodist@cpan.org&gt;</dt>
<dd>

</dd>
</dl>

<h1 id="AUTHORS"><a class="permalink" href="./Test2::API::Context.html#AUTHORS">#</a>AUTHORS</h1>

<dl>

<dt id="Chad-Granum1"><a class="permalink" href="./Test2::API::Context.html#Chad-Granum1">#</a><a id="Chad-Granum-&lt;exodist@cpan.org&gt;1"></a><a id="Chad1"></a><a id="Chad-Granum-exodist-cpan.org1"></a>Chad Granum &lt;exodist@cpan.org&gt;</dt>
<dd>

</dd>
<dt id="Kent-Fredric"><a class="permalink" href="./Test2::API::Context.html#Kent-Fredric">#</a><a id="Kent-Fredric-&lt;kentnl@cpan.org&gt;"></a><a id="Kent"></a><a id="Kent-Fredric-kentnl-cpan.org"></a>Kent Fredric &lt;kentnl@cpan.org&gt;</dt>
<dd>

</dd>
</dl>

<h1 id="COPYRIGHT"><a class="permalink" href="./Test2::API::Context.html#COPYRIGHT">#</a>COPYRIGHT</h1>

<p>Copyright 2020 Chad Granum &lt;exodist@cpan.org&gt;.</p>

<p>This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p>

<p>See <i>http://dev.perl.org/licenses/</i></p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
